import { _decorator, Component, EventKeyboard, input, Input, KeyCode, Node, v3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('RpgPlayer')
export class RpgPlayer extends Component {

    onLoad(): void {
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
    }

    onDestroy(): void {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
    }

    start() {


    }

    private speed: number = 10

    update(deltaTime: number) {
        if (this.vertical == 0 && this.horizonal == 0) {
            return;
        }
        let position = this.node.getPosition()
        this.node.setPosition(position.x + this.speed * this.horizonal,
            position.y + this.speed * this.vertical, position.z)
    }

    private horizonal: number = 0

    private vertical: number = 0

    onKeyDown(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_W:
                this.vertical = 1;
                break;
            case KeyCode.KEY_S:
                this.vertical = -1;
                break;
            case KeyCode.KEY_A:
                this.horizonal = -1;
                break;
            case KeyCode.KEY_D:
                this.horizonal = 1;
                break;
        }
    }

    onKeyUp(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_W:
            case KeyCode.KEY_S:
                this.vertical = 0;
                break;
            case KeyCode.KEY_A:
            case KeyCode.KEY_D:
                this.horizonal = 0;
                break;
        }
    }

}


